﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;

namespace DARE
{
    public class CPhysicCapsule : APhysicObject
    {

    #region fields

        private float m_length;
        private float m_diameter;

    #endregion

    #region properties

        public float Length { get { return m_length; } }
        public float Diameter { get { return m_diameter; } }

    #endregion

        #region ctor

        public CPhysicCapsule()
        {
            Initialize(2.0f, 1.0f);
        }

        public CPhysicCapsule(float length, float diameter)
        {
            Initialize(length, diameter);
        }

        private void Initialize(float length, float diameter)
        {
            m_length = length;
            m_diameter = diameter;

            m_body = new Body();
            m_collisionSkin = new CollisionSkin(m_body);

            m_collisionSkin.AddPrimitive(new Capsule(Vector3.Transform(new Vector3(-0.5f, 0, 0), Matrix.Identity), 
                Matrix.CreateRotationX(MathHelper.PiOver2), diameter / 2.0f, length - diameter), (int)MaterialTable.MaterialID.BouncyNormal);

            m_body.CollisionSkin = m_collisionSkin;
            Vector3 com = SetMass(1.0f);

            m_collisionSkin.ApplyLocalTransform(new Transform(-com, Matrix.Identity));
            m_body.EnableBody();

            m_basicModel = new CCapsuleModel("mCapsule", length, diameter);
        }

        public override APhysicObject Clone()
        {
            CPhysicCapsule clone = new CPhysicCapsule(m_length, m_diameter);

            base.CopyPropertiesTo(clone);
            return clone;
        }

    #endregion

    }
}
